// by Xeno
//#define __FORCE_TRACE__
//#define __FORCE_DEBUG__
//#define __FORCE_INFO__
#define THIS_FILE "tyr_artillery\fn_fire.sqf"
#include <core_macros.h>

TRACE_1("Start",_this);
TMFPROFILERSTART;

_this spawn {
	TRACE_1("Spawn Start",_this);
	private ["_height", "_ari_type", "_radius", "_arix", "_ariy", "_ang", "_posf", "_posb", "_posl", "_posr", "_series", "_x1", "_y1", "_angle", "_strenght", "_i", "_j"
			,"_nos", "_shell", "_xo", "_pos", "_pod", "_obj", "_center_x", "_center_y", "_wp_array", "_ari_target","_ari_avail","_side_arti", "_num_arti"
			, "_aristr", "_topicside", "_topicside2", "_logic", "_ari_vecs", "_ari_side", "_var_str","_rounds","_delay"];
	if (!isServer) exitWith {};

	DEFAULT_PARAM(_player,0,objNull);
	DEFAULT_PARAM(_ari_side,1,west);
	DEFAULT_PARAM(_ari_type,2,"");
	DEFAULT_PARAM(_ari_salvos,3,1);
	DEFAULT_PARAM(_ari_target,4,ARR_3(0,0,0));

	_ari_salvos = parseNumber _ari_salvos;

	//#define __inform_player(text) [2,[server,_player,text],QUOTE(FUNC(informPlayer))] call mso_core_fnc_ExMP;
	#define __inform_player(text) ["TYR",QGVAR(INFORMPLAYER),[server,_player,text],"player"] call IFUNC(rpc);

	if (IS_SIDE(_ari_side)) then {
		_ari_side = str(_ari_side);
	};
	_ari_side = switch(toUpper(_ari_side)) do {
		case "WEST": { "WEST" };
		case "EAST": { "EAST" };
		case "GUER": { "GUER" };
		case "BLUFOR": { "BLUFOR" };
		case "OPFOR": { "OPFOR" };
		case "CIV": { "GUER" };
		default { "WEST" };
	};

	_var_str = format["%1_%2",QGVAR(ARTY_AVAILABLE),_ari_side];
	if (missionNamespace getVariable [_var_str,time] > time) exitWith {
		_delay = ceil((missionNamespace getVariable [_var_str,time]) - time);
		__inform_player(ARR_2("STR_TYR_ARTY_NOTAVAILABLE",_delay))
	};

	if (isNil QGVAR(ARTY_DELAY)) then { GVAR(ARTY_DELAY) = TYR_Artillery_Delay; };
	missionNamespace setVariable [_var_str,(time + GVAR(ARTY_DELAY))];

	sleep 8.123;
	__inform_player(ARR_2("STR_TYR_ARTY_ORDERS_RECEIVED",mapGridPosition _ari_target))

	sleep 8.54;
	_name_shell = getText(configFile/"CfgMagazines"/_ari_type/"displayname");
	__inform_player(ARR_2("STR_TYR_ARTY_FIRING_SHELL",_name_shell))

	sleep 8 + (random 7);

	_ari_vecs = [];
	for "_i" from 0 to 20 do {
		_vvx = missionNamespace getVariable format ["tyr_arty_%1_%2", _ari_side, _i];
		if (!isNil "_vvx") then {
			_ari_vecs set [count _ari_vecs, _vvx];
		};
	};
	_at_pos = [0,0,0];
	if (IS_OBJECT(_ari_target)) then {
		_at_pos = position _ari_target;
	} else {
		_at_pos = _ari_target;
	};
	_at_pos set [2,0];

	TRACE_2("Arty Vecs",_ari_vecs,_at_pos);
	_eta_time = 15 + random(15);
	_inrange = true;
	if (count _ari_vecs > 0) then {
		_eta_time = (_ari_vecs select 0) getArtilleryETA [_at_pos, _ari_type];
		_inrange = _at_pos inRangeOfArtillery [[(_ari_vecs select 0)], _ari_type];
	};
	TRACE_2("ETA",_eta_time,_inrange);
	if (!_inrange) exitWith {
		__inform_player("STR_TYR_ARTY_NOTINRANGE")
	};

	_magammoconf = getText(configFile/"CfgMagazines"/_ari_type/"ammo");
	_ammoconf = configFile/"CfgAmmo"/_magammoconf;
	_is_flare = getText(_ammoconf/"effectFlare") == "CounterMeasureFlare";
	_is_smoke = getText(_ammoconf/"submunitionAmmo") == "SmokeShellArty";

	_series = 0;
	_rounds = 1;
	for "_series" from 1 to _ari_salvos do {
		if (count _ari_vecs > 0) then {
			if (_is_smoke) then {
				_rounds = 2;
			};
			for "_i" from 0 to _rounds do {
				{
					_radius = (GVAR(RADIUS) select 0) + random (GVAR(RADIUS) select 1);
					_angle = floor random 360;
					_x1 = (_at_pos select 0) - ((random _radius) * sin _angle);
					_y1 = (_at_pos select 1) - ((random _radius) * cos _angle);
					
					_x setVehicleAmmo 1;
					_x setFuel 1;
					_x setDamage 0;
					
					_x doArtilleryFire [[_x1, _y1, 0], _ari_type, 1];
					sleep 0.2;
				} forEach _ari_vecs;
			};
		} else {
			// Nothing to do.. there are no vecs!
		};
		__inform_player(ARR_3("STR_TYR_ARTY_FIRED",_series,str(round(_eta_time))))

		sleep _eta_time;
		__inform_player(ARR_2("STR_TYR_ARTY_SPLASH",_series))

		if (count _ari_vecs > 0) then {
			// Nothing to do, the vehicles already fired the rounds!
		} else {
			_height = 150;
			_shell = objNull;
			_rounds = 3;
			if (!_is_flare && {!_is_smoke}) then {
			
			} else {
				if (_is_flare) then {
					_height = 200;
				} else {
					_rounds = 6;
					_height = 1;		
				};
			};
			for "_i" from 0 to _rounds do {
				_radius = (GVAR(RADIUS) select 0) + random (GVAR(RADIUS) select 1);
				_angle = floor random 360;
				_x1 = (_at_pos select 0) - ((random _radius) * sin _angle);
				_y1 = (_at_pos select 1) - ((random _radius) * cos _angle);
				sleep 0.2;
				_at_pos set [2,_height];
				_shell = createVehicle [_magammoconf, [_x1, _y1, _height], [], 0, "NONE"];
				if (_is_flare) then {
					_shell setVelocity [0,0,-20];
				} else {
					_shell setVelocity [0,0,-150];
				};
				sleep 0.5;
			};		
		};
		
		if (_series < _ari_salvos) then {
			__inform_player(ARR_2("STR_TYR_ARTY_RELOAD",_series))
			sleep (GVAR(ARTY_RELOAD_DELAY) + random (GVAR(ARTY_RELOAD_DELAY) / 3));
		};
	};

	sleep 3;

	__inform_player("STR_TYR_ARTY_COMPLETED")

	missionNamespace setVariable [_var_str,(time + GVAR(ARTY_DELAY))];
	TRACE("Spawn End");
};

TRACE("End");
TMFPROFILERSTOP;